define(function(require, exports, module) {

    var AV = require('leancloud'),
        _ = require('underscore'),
        Framework7 = require('framework7');

    Framework7.prototype.plugins.gomac = function (app, params) {
        // exit if not enabled
        if (!params) return;

        //appInit事件响应
        var _appInit = function() {
            console.log('appInit', app);
            //登录
            $('.login-screen').on('open', function(e){
                var SignIn = require('app/sign-in');
                new SignIn({ gomac: app });
            });

            //添加聚会
            $('.popup-meeting').on('open', function(e){
                var PlanFactory = require('app/plan/create'),
                    PlanModel = require('model/plan'),
                    PlanCollection = require('collection/plan');
                var model = new PlanModel;
                model.collection = new PlanCollection;
                new PlanFactory({
                    model: model,
                    gomac: app 
                });
            });
            
        };

        //addView事件响应
        var _addView = function(view) {
            console.log('addView', view);
            //登录判断
            if( view.main ) {
                if( !AV.User.current() ) {
                    app.loginScreen();
                }
                else {
                    app.mainView.router.loadPage('pages/home.html');
                }
            }
            //app.mainView.router.loadPage('pages/home.html');
            //app.loginScreen();
        };

        var _pageInit = function(pageData) {
            console.log('pageInit', pageData);
            var Gomac = {};
            //if(pageData.from === 'right') {
                switch(pageData.name) {
                    case 'home':
                        if(pageData.from === 'right') {
                            var Gomac = require('app/plan');
                            var gomac = new Gomac({ 
                                gomac: app 
                            });
                            gomac.render();
                        }
                        break;
                    case 'planView':
                        var Gomac = require('app/plan/view');
                        var gomac = new Gomac({
                            gomac: app,
                            objectId: pageData.query.id
                        });
                        break;
                    case 'contacts':
                        var Gomac = require('app/contact');
                        var gomac = new Gomac({
                            gomac: app
                        });
                        gomac.render();
                        break;
                    case 'contactView':
                        var Gomac = require('app/contact/view');
                        var gomac = new Gomac({
                            gomac: app,
                            objectId: pageData.query.id
                        });
                        break;
                    case 'contactSearch':
                        var Gomac = require('app/contact/search');
                        var gomac = new Gomac({
                            gomac: app
                        });
                        break;
                    default:
                        console.log(pageData.name);
                        break;
                }
            //}
        }

        return {
            hooks: {
                appInit: _appInit,
                pageInit: _pageInit,
                addView: _addView/*,
                navbarInit: function (navbar, pageData) {
                    console.log('navbarInit', navbar, pageData);
                },
                pageBeforeInit: function (pageData) {
                    console.log('pageBeforeInit', pageData);
                },
                pageBeforeAnimation: function (pageData) {
                    console.log('pageBeforeAnimation', pageData);
                },
                pageAfterAnimation: function (pageData) {
                    console.log('pageAfterAnimation', pageData);
                },
                pageBeforeRemove: function (pageData) {
                    console.log('pageBeforeRemove', pageData);
                },
                loadPage: function (view, url, content) {
                    console.log('loadPage', view, url, content);
                },
                goBack: function (view, url, preloadOnly) {
                    console.log('goBack', view, url, preloadOnly);
                },
                swipePanelSetTransform: function (views, panel, percentage) {
                    console.log('swipePanelSetTransform', views, panel, percentage);
                }*/
            }
        };
    };

    module.exports = Framework7;
});

